/**
 * Description: 
 * File: write_file.cpp |Time: 2022-05-09 15:05
 */

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, const char *argv[])
{
    /**
     * ios::in -------- input
     * ios::out ------- output
     * ios::binary ---- binary
     * ios::ate ------- at the end
     * ios::app ------- append
     * ios::trunc ----- truncate
     */
    {
        fstream fs;

        fs.open("test.txt", ios::out | ios::trunc);
        if(!fs) {
            cout << "file creation failed." << endl;
            return -1;
        }
        
        fs << "I am the first line.\n";
        fs << "I am the second line.\n";

        fs.close();
    }
    
    {
        // 在ifstream ofstream fstream 的构造函数中已经调用了open函数
        // 所以可以直接在构造对象时传入open参数
        fstream fs("test.bin", ios::out | ios::binary);

        if(fs.is_open() == false) {
            cout << "test.bin open failed.";
            return -1;
        }

        fs << "I am the first line.\n";
        fs << "I am the second line.\n";

        fs.close();
    }
    
    return 0;
}


